package com.yata.utils.controller;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

/**
 * Created by hecy on 2017/12/27.
 */
public class HttpClient4Tools {

    //发送一个GET请求
    public static String get(String path) throws Exception {
        HttpURLConnection httpConn = null;
        BufferedReader in = null;
        try {
            URL url = new URL(path);
            httpConn = (HttpURLConnection) url.openConnection();

            //读取响应
            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                StringBuffer content = new StringBuffer();
                String tempStr = "";
                in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                while ((tempStr = in.readLine()) != null) {
                    content.append(tempStr);
                }
                return content.toString();
            } else {
                throw new Exception("请求出现了问题!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            in.close();
            httpConn.disconnect();
        }
        return null;
    }

    //发送一个GET请求,参数形式key1=value1&key2=value2...
    public static String post(String path, String params) throws Exception {
        HttpURLConnection httpConn = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            URL url = new URL(path);

            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("POST");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);

            //发送post请求参数
            out = new PrintWriter(httpConn.getOutputStream());
            out.println(params);
            out.flush();

            //读取响应
            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                StringBuffer content = new StringBuffer();
                String tempStr = "";
                in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                while ((tempStr = in.readLine()) != null) {
                    content.append(tempStr);
                }
                return content.toString();
            } else {
                throw new Exception("请求出现了问题!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            in.close();
            out.close();
            httpConn.disconnect();
        }
        return null;
    }


    public String fanyiByBaidu(String msg) {
        StringBuffer path = new StringBuffer("http://api.fanyi.baidu.com/api/trans/vip/translate?");
        String api = "20171227000109677";
        String key = "gNsH2N0S6vDGfPpIBvKS";
        int salt = new Random().nextInt();
        path.append("from=auto&to=auto&");
        path.append("appid=20171227000109677&");
        path.append("q=" + msg + "&");
        path.append("salt=" + salt + "&");
        String temp = api + msg + salt + key;
        path.append("sign=" + new Tools().MD5(temp, 32) + "");

        try {
            return get(path.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String ss = new HttpClient4Tools().fanyiByBaidu("代码");
        System.out.println(ss);
        JSONObject json = new JSONObject(ss);
        JSONArray jsonArray = json.getJSONArray("trans_result");
        System.out.println("jsonArray：" + jsonArray);
        JSONObject jj = (JSONObject)jsonArray.get(0);
        System.out.println(jsonArray);
        System.out.println(jj.get("dst"));
     }
}

